package net.lcheng.sellerCenter.controller;

import net.lcheng.model.User;
import net.lcheng.model.WxShop;
import net.lcheng.model.WxShopBanner;
import net.lcheng.service.UserService;
import net.lcheng.service.WxShopBannerService;
import net.lcheng.service.WxShopService;
import net.lcheng.vo.WxShopBannerModel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by zwr on 2016/7/4 0004.
 */
@Controller
public class WxShopBannerContoller {
    @Autowired
    private UserService userService;
    @Autowired
    private WxShopService wxShopService;
    @Autowired
    private WxShopBannerService wxShopBannerService;
  /**店铺Banner设置*/
    @RequestMapping(value = "shop_banner", method = RequestMethod.GET)
    public String shopBannerGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()) {
            String userPhoneNum = currentUser.getSession().getAttribute("UserPhoneNum").toString();
            //获取用户信息
            User user = userService.getUserByPhone(userPhoneNum);
            //获取店铺id
            WxShop shop = wxShopService.getShopInforByUserId(user.getUserId());
            if (shop==null){
                model.addAttribute("shopId", 1);
            }else if (shop!=null){
                List<WxShopBanner>wxShopBanner=wxShopBannerService.getBannerListByShopId(shop.getShopId());
                model.addAttribute("wxShopBanner", wxShopBanner);
            }
        }
        return "shop_banner";
    }
    /**添加BannerGet*/
    @RequestMapping(value = "add/shop_banner",method = RequestMethod.GET)
    public  String addShopBannerGet(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()) {
            String userPhoneNum = currentUser.getSession().getAttribute("UserPhoneNum").toString();
            //获取用户信息
            User user = userService.getUserByPhone(userPhoneNum);
            //获取店铺id
            WxShop shop = wxShopService.getShopInforByUserId(user.getUserId());
            if (shop == null) {
                model.addAttribute("shop", 1);
            }
            model.addAttribute("WxShopBannerModel", new WxShopBannerModel());
        }
        return "add_banner";
    }
    /**添加BannerPost*/
    @RequestMapping(value = "add/shop_banner",method = RequestMethod.POST)
    public  String addShopBannerPst(Model model, @Valid @ModelAttribute("WxShopBannerModel") WxShopBannerModel wxShopBannerModel, BindingResult result){
        if (result.hasErrors()){
            return "add_banner";
        }
        //获取用户信息
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()) {
            String userPhoneNum = currentUser.getSession().getAttribute("UserPhoneNum").toString();
            //获取用户信息
            User user = userService.getUserByPhone(userPhoneNum);
            //获取店铺id
            WxShop shop = wxShopService.getShopInforByUserId(user.getUserId());
            WxShopBanner wxShopBanner = new WxShopBanner();
            wxShopBanner.setBannerId(UUID.randomUUID().toString());
            wxShopBanner.setWxShopShopId(shop.getShopId());
            wxShopBanner.setImage(wxShopBannerModel.getImage());
            wxShopBanner.setLink(wxShopBannerModel.getLink());
            wxShopBanner.setOrderNum(wxShopBannerModel.getOrderNum());
            wxShopBanner.setCreateOn(new Date());
            wxShopBanner.setStatus(1);
            int returnResult = wxShopBannerService.addBanner(wxShopBanner);
            if(returnResult>0){
                return "redirect:/shop_banner";
            }else
                return "redirect:/shop_banner";
        }
        return "redirect:/shop_banner";
    }
    /**编辑BannerGet*/
    @RequestMapping(value = "edit/shop_banner",method = RequestMethod.GET)
    public String editShopBannerGet(Model model, @RequestParam String id){
       WxShopBanner wxShopBanner=wxShopBannerService.getBannerByBannerId(id);
        //把wxShopBanner传递到bannerModel
        WxShopBannerModel bannerModel =  new WxShopBannerModel();
        bannerModel.setBannerId(wxShopBanner.getBannerId());
        bannerModel.setImage(wxShopBanner.getImage());
        bannerModel.setOrderNum(wxShopBanner.getOrderNum());
        bannerModel.setLink(wxShopBanner.getLink());
        model.addAttribute("wxShopBanner",wxShopBanner);
        model.addAttribute("WxShopBannerModel",bannerModel);
        return "edit_banner";
    }
    /**编辑BannerPost*/
    @RequestMapping(value = "edit/shop_banner",method = RequestMethod.POST)
    public  String editShopBannerPost(Model model,@Valid @ModelAttribute("WxShopBannerModel")WxShopBannerModel wxShopBannerModel,BindingResult result) {
       if (result.hasErrors()){
           return "edit_banner";
       }
        //更新Banner
        WxShopBanner wxShopBanner = new WxShopBanner();
        wxShopBanner.setBannerId(wxShopBannerModel.getBannerId());
        wxShopBanner.setLink(wxShopBannerModel.getLink());
        wxShopBanner.setImage(wxShopBannerModel.getImage());
        wxShopBanner.setOrderNum(wxShopBannerModel.getOrderNum());
        wxShopBanner.setModifyOn(new Date());
        int returnResult =  wxShopBannerService.edit(wxShopBanner);
        if(returnResult>0){
            return "redirect:/shop_banner";
        }else
        return "edit_banner";
    }
    /**删除Banner*/
    @RequestMapping(value = "delete/shop_banner",method = RequestMethod.GET)
    public String deleteShopBannerGet(Model model,@RequestParam String id){
        WxShopBanner wxShopBanner =new WxShopBanner();
        wxShopBanner.setBannerId(id);
        wxShopBanner.setStatus(0);
        wxShopBannerService.modifyStatus(wxShopBanner);
        return "redirect:/shop_banner";
    }

}